//
//  Global.m
//  ShoppingExperience
//
//  Created by LiYinDi on 15-1-5.
//  Copyright (c) 2015年 RuiBen. All rights reserved.
//

#import "Global.h"
#import "CommonCrypto/CommonDigest.h"
static NSMutableString *tokenStr;

static NSMutableString *loginGuideCodeStr;
static NSMutableString *loginGuideNameStr;
static NSMutableString *storeNameStr;
static NSMutableString *loginGuideNameStr;
static NSMutableString *ifShowTextHudStr;


static NSUInteger shopGuideID;
static NSUInteger _stockID;
static NSUInteger _scanID;
static NSMutableString *_urlStr;
static NSMutableString *upYunStr;
static NSMutableString *polycyStr;
static NSMutableString *signatureStr;

static NSMutableString *headIconString;
static NSUInteger memberRegisterID;
static NSUInteger shopAffairID;
static NSUInteger myAccountID;
static NSUInteger shopCartID;
static NSUInteger _orderID;
static NSUInteger _productID;
static NSUInteger styleDetailID;
static NSUInteger homeID;
static CGFloat keyboardHeight;
//static CGFloat depositAmount;
static MBProgressHUD *_textHUD;
static MBProgressHUD *HUD;

static NSMutableArray *mCustomArray;
static NSDictionary *mProductShopCartDict;


@implementation Global
+(void)loginViewController:(UIViewController *)vc noteStr:(NSString *)noteStr
{
    GlobalViewController *controller=[[GlobalViewController alloc]init];
   controller.ifShowNote=@"Y";
    controller.noteStr=noteStr;
    [vc.navigationController pushViewController:controller animated:YES];
}
+(void)loginViewController:(UIViewController *)vc
{
    UIViewController *controller=[[GlobalViewController alloc]init];
    [vc.navigationController pushViewController:controller animated:YES];

}
+(NSUInteger)getProductID
{
    return _productID;
}
+(void)setProductID:(NSUInteger)productID
{
    _productID=productID;
}
+(NSUInteger)getScanID
{
    return _scanID;
}
+(void)setScanID:(NSUInteger)scanID
{
    _scanID=scanID;
}

+(NSUInteger)getOrderID
{
     return _orderID;
}
+(void)setOrderID:(NSUInteger)orderID
{
    _orderID=orderID;
}
+(NSUInteger)getStockID
{
    return _stockID;
}
+(void)setStockID:(NSUInteger)stockID
{
    _stockID=stockID;
}
+(NSUInteger)getHomeCustomID
{
    return homeID;
}
+(void)setHomeCustomID:(NSUInteger)customID
{
    homeID=customID;
}

+(NSUInteger)getShopGuideID
{
    return shopGuideID;
}
+(void)setShopGuideID:(NSUInteger)guideID
{
    shopGuideID=guideID;
}


+(NSUInteger)getShopAffairID
{
    return shopAffairID;
}
+(void)setShopAffairID:(NSUInteger)affairID
{
    shopAffairID=affairID;
}
+(NSUInteger)getMemberRegisterID
{
    return memberRegisterID;
}
+(void)setMemberRegisterID:(NSUInteger)memberID;
{
    memberRegisterID=memberID;
}

+(NSUInteger)getMyAccountID
{
    return myAccountID;
}
+(void)setMyAccountID:(NSUInteger)accountID
{
    myAccountID=accountID;
}
+(CGFloat)getKeyboardHeight{
    return keyboardHeight;
}
+(void)setKeyboardHeight:(CGFloat)keyboardH
{
    keyboardHeight=keyboardH;
}
+(NSUInteger)getShopCartID
{
    return shopCartID;
}
+(void)setShopCartID:(NSUInteger)cartID
{
    shopCartID=cartID;
}
+(NSUInteger)getStyleDetailID
{
    return styleDetailID;
}
+(void)setStyleDetailID:(NSUInteger)styleID
{
    styleDetailID=styleID;
}

+(void)setCustomAddArray:(NSArray *)customArray
{
    if(mCustomArray==nil)
    {
        mCustomArray=[[NSMutableArray alloc] init];
    }
    [mCustomArray setArray:customArray];
}
+(NSArray *)getCustomAddArray
{
    return mCustomArray;
}
+(void)setProductShopCartDict:(NSDictionary *)selectedProductDict
{
    if (mProductShopCartDict==nil) {
        mProductShopCartDict=[[NSDictionary alloc] init];
    }
   // [mProductShopCartDict setDictionary:selectedProductDict];
    mProductShopCartDict=selectedProductDict;
}
+(NSDictionary *)getProductShopCartDict
{
    return mProductShopCartDict;
}

+(NSString *)getHeadIconStr
{
    return headIconString;
}
+(void)setHeadIcon:(NSString *)iconStr
{
    if (headIconString==nil) {
        headIconString=[[NSMutableString alloc] init];
    }
    [headIconString setString:iconStr];
}

+(NSString *)getStoreName
{
    return storeNameStr;
}
+(void)setStoreName:(NSString *)storeName
{
    if (storeNameStr==nil) {
        storeNameStr=[[NSMutableString alloc] init];
    }
    [storeNameStr setString:storeName];

}
+(NSString *)getIfShowTextHudStr
{
    return ifShowTextHudStr;
}
+(void)setIfShowTextHudStr:(NSString *)ifShowHudStr
{
    if (ifShowTextHudStr==nil) {
        ifShowTextHudStr=[[NSMutableString alloc] init];
    }
    [ifShowTextHudStr setString:ifShowHudStr];

}
+(NSString *)getLoginGuideName
{
    return loginGuideNameStr;
}
+(void)setLoginGuideName:(NSString *)guideName
{
    if (loginGuideNameStr==nil) {
        loginGuideNameStr=[[NSMutableString alloc] init];
    }
    [loginGuideNameStr setString:guideName];
}
+(NSString *)getLoginToken
{
    return tokenStr;
}
+(void)setLoginToken:(NSString *)token
{
    if (tokenStr==nil) {
        tokenStr=[[NSMutableString alloc] init];
    }
    [tokenStr setString:token];
}

+(NSString *)getUpYunUrl
{
     return upYunStr;
}
+(void)setUpYunUrl:(NSString *)urlStr
{
    if (upYunStr==nil) {
        upYunStr=[[NSMutableString alloc] init];
    }
    [upYunStr setString:urlStr];
}
+(NSString *)getPolicyStr
{
    return polycyStr;
}
+(void)setPolicyStr:(NSString *)urlStr
{
    if (polycyStr==nil) {
        polycyStr=[[NSMutableString alloc] init];
    }
    [polycyStr setString:urlStr];
}
+(NSString *)getSignatureStr
{
    return signatureStr;
}
+(void)setSignatureStr:(NSString *)urlStr
{
    if (signatureStr==nil) {
        signatureStr=[[NSMutableString alloc] init];
    }
    [signatureStr setString:urlStr];
}

+(NSString *)getLoginUrl
{
    return _urlStr;
}
+(void)setLoginUrl:(NSString *)urlStr
{
    if (_urlStr==nil) {
        _urlStr=[[NSMutableString alloc] init];
    }
    [_urlStr setString:urlStr];
}
+(NSString *)getGuideCodeStr
{
    return loginGuideCodeStr;
}
+(void)setGuideCodeStr:(NSString *)codeStr
{
    if (loginGuideCodeStr==nil) {
        loginGuideCodeStr=[[NSMutableString alloc] init];
    }
    [loginGuideCodeStr setString:codeStr];
}

+(NSString *)getGuideNameStr
{
    return loginGuideNameStr;
}
+(void)setGuideNameStr:(NSString *)nameStr
{
    if (loginGuideNameStr==nil) {
        loginGuideNameStr=[[NSMutableString alloc] init];
    }
    [loginGuideNameStr setString:nameStr];
}
+(UIColor *)parseStatusColor:(NSString *)statusStr
{
    if ([statusStr isEqualToString:@"未收银"]||[statusStr isEqualToString:@"未上交"]||[statusStr isEqualToString:@"未到货"]||[statusStr isEqualToString:@"未确定"]) {
        return [UIColor colorWithRed:177.0f/255.0f green:0
                                blue:4.0f/255.0f alpha:1.0f];
    }
    else if ([statusStr isEqualToString:@"已发货"]||[statusStr isEqualToString:@"已收定金"])
    {
        return [UIColor colorWithRed:254.0f/255.0f green:181.0f/255.0f
                                blue:10.0f/255.0f alpha:1.0f];
    }
    else if ([statusStr isEqualToString:@"已结算"]||[statusStr isEqualToString:@"已收银"]||[statusStr isEqualToString:@"已上交"]||[statusStr isEqualToString:@"已使用"]||[statusStr isEqualToString:@"已确定"]||[statusStr isEqualToString:@"已到货"])
    {
        return [UIColor colorWithRed:130.0f/255.0f green:201.0f/255.0f
                                blue:63.0f/255.0f alpha:1.0f];
    }
    else if ([statusStr isEqualToString:@"已关闭"]||[statusStr isEqualToString:@"已取消"])
    {
        return [UIColor colorWithRed:1.0f/255.0f green:21.0f/255.0f
                                blue:78.0f/255.0f alpha:1.0f];
    }
    else if ([statusStr isEqualToString:@"未使用"]||[statusStr isEqualToString:@"未发货"])
    {
       
        return [UIColor colorWithRed:20.0f/255.0f green:158.0f/255.0f
                                blue:235.0f/255.0f alpha:1.0f];
    }
    
    else if ([statusStr isEqualToString:@"下柜"]||[statusStr isEqualToString:@"部分到货"]||[statusStr isEqualToString:@"部分上交"])
    {
        return [UIColor colorWithRed:199.0f/255.0f green:156.0f/255.0f
                                blue:44.0f/255.0f alpha:1.0f];
    }
    else if ([statusStr isEqualToString:@"在途"]||[statusStr isEqualToString:@"上柜"])
    {
        return [UIColor colorWithRed:100.0f/255.0f green:130.0f/255.0f
                                blue:46.0f/255.0f alpha:1.0f];
    }
    


    return [UIColor colorWithRed:177.0f/255.0f green:0
                            blue:4.0f/255.0f alpha:1.0f];
}
+(UIColor *)parseColor:(NSString *)hexColor {
    if(![[hexColor substringToIndex:1] isEqualToString:@"#"])
        return [UIColor clearColor];
    
    unsigned int alpha,red, green, blue;
    NSRange range = NSMakeRange(0, 2);
    NSString *scanStr = nil;
    
    if(hexColor.length==7){
        range.location = -1;
        alpha = 255;
    }else if(hexColor.length==9){
        range.location = 1;
        scanStr = [hexColor substringWithRange:range];
        [[NSScanner scannerWithString:scanStr]scanHexInt:&alpha];
    }
    
    range.location += 2;
    scanStr = [hexColor substringWithRange:range];
    [[NSScanner scannerWithString:scanStr]scanHexInt:&red];
    range.location += 2;
    scanStr = [hexColor substringWithRange:range];
    [[NSScanner scannerWithString:scanStr]scanHexInt:&green];
    range.location += 2;
    scanStr = [hexColor substringWithRange:range];
    [[NSScanner scannerWithString:scanStr]scanHexInt:&blue];
    
    return colorFromARGB(alpha, red, green, blue);
}

UIColor *colorFromARGB(CGFloat alpha,CGFloat red,CGFloat green,CGFloat blue){
    return [UIColor colorWithRed:red/255.0f green:green/255.0f
                            blue:blue/255.0f alpha:alpha/255.0f];
}



+(void)showTextHud:(NSString*)message view:(UIView *)view{
    _textHUD = [MBProgressHUD showHUDAddedTo:
                view animated:YES];
    [_textHUD setMode:MBProgressHUDModeText];
    [_textHUD setLabelText:message];
    [_textHUD setMargin:10.f];
    [_textHUD setYOffset:view.frame
     .size.height/2-200];
    [_textHUD setXOffset:view.frame.size.width/2-140];
    [_textHUD setRemoveFromSuperViewOnHide:YES];
    [_textHUD setUserInteractionEnabled:NO];
    [_textHUD hide:YES afterDelay:3];
}

+(void)showIndeterminateHud:(NSString*)text delay:(int)delay view:(UIView *)view{
    HUD = [MBProgressHUD showHUDAddedTo:
           view animated:YES];
    
    [HUD setMode:MBProgressHUDModeIndeterminate];
    [HUD setRemoveFromSuperViewOnHide:YES];
    [HUD setXOffset:view.frame.size.width/2-110];
    [HUD hide:YES afterDelay:delay];
    [HUD setLabelText:text];
    [HUD setMargin:10.f];
    // NSLog(@"show hud:%@",HUD);
}
+(void)showTextHudSub:(NSString*)message view:(UIView *)view{
   
    if ([[self getIfShowTextHudStr]isEqualToString:@"N"]) {
        return;
         }
  
     // if (_textHUD == nil) {
    _textHUD = [MBProgressHUD showHUDAddedTo:
                view animated:YES];
    [_textHUD setMode:MBProgressHUDModeText];
    [_textHUD setLabelText:message];
    [_textHUD setMargin:10.f];
   
    [_textHUD setYOffset:view.frame
     .size.height/2-430];
    
    // [_textHUD setXOffset:240];
    [_textHUD setRemoveFromSuperViewOnHide:YES];
    [_textHUD setUserInteractionEnabled:NO];
    [_textHUD hide:YES afterDelay:1.5];
   // }
   
}
+(CGFloat)visibleKeyboardHeight {
    
    UIWindow *keyboardWindow = nil;
    
    for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
        
        if(![[testWindow class] isEqual:[UIWindow class]]) {
            
            keyboardWindow = testWindow;
            
            break;
            
        }
        
    }
    
    
    
    for (__strong UIView *possibleKeyboard in [keyboardWindow subviews]) {
        
        //if([possibleKeyboard isKindOfClass:NSClassFromString(@"UIPeripheralHostView")] || [possibleKeyboard isKindOfClass:NSClassFromString(@"UIKeyboard")])
            
            return possibleKeyboard.bounds.size.height;
        
    }
    
    
    
    return 0;
    
}
+(void)hideTextHudSub{
     if (_textHUD != nil) {
         [_textHUD hide:YES afterDelay:0.0001];
        // [_textHUD removeFromSuperview];
     }
}
+(void)showIndeterminateHudSub:(NSString*)text delay:(int)delay view:(UIView *)view
{
    //if (HUD==nil) {
    
    HUD = [MBProgressHUD showHUDAddedTo:
           view animated:YES];
    
    [HUD setMode:MBProgressHUDModeIndeterminate];
    [HUD setRemoveFromSuperViewOnHide:YES];
    // [HUD setXOffset:view.frame.size.width/2-200];
    [HUD hide:YES afterDelay:delay];
    [HUD setLabelText:text];
    [HUD setMargin:10.f];
   // }
}
+(void)hideProgressHUDInternal:(UIView *)view{
    
    MBProgressHUD *hud = [MBProgressHUD HUDForView:view];
    if (hud != nil) {
        
        [hud removeFromSuperview];
        
        
    }
}

+ (float) heightForTextView: (UITextView *)textView WithText: (NSString *) strText{
    float fPadding = 16.0; // 8.0px x 2
    CGSize constraint = CGSizeMake(textView.contentSize.width - fPadding, CGFLOAT_MAX);
    
    CGSize size = [strText sizeWithFont: textView.font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    
    float fHeight = size.height + 16.0;
    
    return fHeight;
}
+ (void) resizeForLable: (UILabel *)label WithText: (NSString *) labelText parentView:(UIView *)parentView
{
    CGRect rect = [label.text boundingRectWithSize:CGSizeMake(parentView.frame.size.width-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: label.font} context:nil];
    
    //3、设置UILabel最大显示行数（＝0时不限制）；
   label.numberOfLines = 0;
    
    //4、用计算出来的CGSize/CGRect设置UILabel的frame。
    //iOS6
    //  label.frame = CGRectMake(label.frame.origin.x , label.frame.origin.y, size.width, size.height);
    //iOS7以后
   label.frame = CGRectMake(label.frame.origin.x , label.frame.origin.y, rect.size.width, label.frame.size.height);
}
+(NSString *)floatConvertPercentile:(float)floatValue
{
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:floatValue]];
    return formattedNumberString;
}
+(NSString *) md5: (NSString *) inPutText
{
    const char *cStr = [inPutText UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
    
    return [[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
             result[0], result[1], result[2], result[3],
             result[4], result[5], result[6], result[7],
             result[8], result[9], result[10], result[11],
             result[12], result[13], result[14], result[15]
             ] lowercaseString];
}

@end
